<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Eventos_Model
 *
 * @author marcelo.diaz
 */
class Eventos_Model extends Model{	
    function Eventos_Model(){
      parent::Model(6);
    }
	
  /**
   * Get all events from the last year
   * @return array
   */
  function _all(){    
  	 $query = $this->db->query("
								SELECT 
									e.name,
									e.description,
									e.img,
									e.icon,
									e.fecha,
									m.name as m_name,
									m.lat,
									m.lng,
									m.address,
									m.city,
									m.description as m_description,
									m.img as m_img,
									m.icon as m_icon
								FROM evento as e
								LEFT OUTER JOIN marker as m ON e.place = m.id_marker
			 					WHERE e.fecha >= CURDATE() - INTERVAL 6 MONTH  	 		
  	 							ORDER BY e.fecha DESC
  	 							");
	 $places = $query->result_array();	 
	 foreach ($places as $k => $data){
	 	//we MUST detect if icon and image are local, and add them the correct absolute path
	 	$places[$k]['icon'] = base_url()."images/icons/".$data['icon'];
	 	if(strpos($data['img'], ':') === false){
	 		$places[$k]['img'] = $places[$k]['icon'];
	 	}
	 }	 
	 //print_r($places);
     return $places;
  }

  /**
   * 
   * @param String $place: name of the event to search
   */
  function get_place($place){
  	$related = explode('-', $place);
	$words = mysql_real_escape_string($place);
  	$query = $this->db->query("
								SELECT 
									e.name,
									e.description,
									e.img,
									e.icon,
									e.fecha,
									m.name as m_name,
									m.lat,
									m.lng,
									m.address,
									m.city,
									m.description as m_description,
									m.img as m_img,
									m.icon as m_icon
								FROM evento as e
								LEFT OUTER JOIN marker as m ON e.place = m.id_marker  			
  								WHERE e.name like '$words' 
				  			");
  	//$row = $query->row();
  	$row = $query->row_array();
  	if(empty($row)){
  		return false;
  	}else{
  		//we MUST detect if icon and image are local, and add them the correct absolute path
  		$row['icon'] = base_url()."images/icons/".$row['icon'];
  		if(strpos($row['img'], ':') === false){
  			$row['img'] = $row['icon'];
  		}
  		//get Related items
  		foreach ($related as $k=>$w){
  			$related[$k] = "marker.name like '%".$w."%'";
  		}
  		$where = implode(" OR ", $related);
  		$query = $this->db->query("SELECT *
  				FROM marker
  				WHERE ($where) AND
  						 aproved = 1 AND
  						(marker.name NOT LIKE '$place')
  				ORDER BY marker.name
  				");
  		$relates = $query->result_array();
  		//we MUST detect if icon and image are local, and add them the correct absolute path
  		foreach ($relates as $k => $v){
  		$relates[$k]['icon'] = base_url()."images/icons/".$v['icon'];
  		if(strpos($v['img'], ':') === false){
  		$relates[$k]['img'] = $relates[$k]['icon'];
  		}
  		}
  		$row['related'] = $relates;  		
  		return $row;  		
  	}
  }    
}
?>
